package com.atguigu.config.websocket1;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 * 回话拦截器
 * @author nrzt
 *	HandshakeInterceptor
 */
@Component("myInterceptor")
public class MyHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
	// 初次握手访问前
	@Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        if (request instanceof ServletServerHttpRequest) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String sessionId=servletRequest.getSession().getId();//获取浏览器的sessionid
            //UserPo user=(UserPo)servletRequest.getSession().getAttribute("user");
            //String userId = user.getUserId();
            //attributes.put("userId", userId);
            //servletRequest.getSession().setAttribute("userId", userId);
        }

        //return super.beforeHandshake(request, response, wsHandler, attributes);
        return true;
    }
	// 初次握手访问后
    @Override
    public void afterHandshake(ServerHttpRequest request,
                               ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {
        
    }
}
